package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.bc.mapper.BcSingleLocationsMapper;
import com.cloud.bc.model.entity.BcCategoryDistricts;
import com.cloud.bc.model.entity.BcSingleLocations;
import com.cloud.bc.model.request.NearbyRequest;
import com.cloud.bc.model.response.NearbyResponse;
import com.cloud.bc.service.BcCategoryDistrictsService;
import com.cloud.bc.service.BcSingleLocationsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 单身位置
 *
 * @author douhaichao code generator
 * @date 2021-01-16 20:46:38
 */
@Service
@AllArgsConstructor
public class BcSingleLocationsServiceImpl extends ServiceImpl<BcSingleLocationsMapper, BcSingleLocations> implements BcSingleLocationsService {

    private final BcCategoryDistrictsService bcCategoryDistrictsService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcSingleLocations selectOne(BcSingleLocations req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcSingleLocations>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcSingleLocations> selectAll(BcSingleLocations req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcSingleLocations> selectPage(Page page, BcSingleLocations req) {
        IPage<BcSingleLocations> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcSingleLocations>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcSingleLocations> selectPageByLike(Page page, BcSingleLocations req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (!StrUtil.isEmpty(req.getDistrict())) {
            queryWrapper.like("district", req.getDistrict());
        }

        if (!StrUtil.isEmpty(req.getDistrictCn())) {
            queryWrapper.like("district_cn", req.getDistrictCn());
        }

        if (!StrUtil.isEmpty(req.getLatitude())) {
            queryWrapper.like("latitude", req.getLatitude());
        }

        if (!StrUtil.isEmpty(req.getLongitude())) {
            queryWrapper.like("longitude", req.getLongitude());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (null != req.getUpdateTime()) {
            queryWrapper.eq("update_time", req.getUpdateTime());
        }
        IPage<BcSingleLocations> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcSingleLocations req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public boolean upsert(BcSingleLocations req) {
        //1上传自己的坐标
        //1-1查询坐标是否存在，不存在添加,存在创建
        int count = this.count(new BcSingleLocations().setUserId(req.getUserId()));
//        req.setLatitude(locationResp.getLat());
//        req.setLongitude(locationResp.getLng());
        String city = req.getDistrictCn();
        setCityData(req, city);
        boolean result=false;
        if (count <= 0) {
            //不存在创建
            result=baseMapper.insert(req)>0;
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", req.getUserId());
            result=baseMapper.update(req, queryWrapper)>0;
        }
        return result;
    }

    private void setCityData(BcSingleLocations req, String city) {
        BcCategoryDistricts bcDistrictsCity = new BcCategoryDistricts();
        bcDistrictsCity.setName(city.replace("市", ""));
        List<BcCategoryDistricts> bcCategoryDistricts = bcCategoryDistrictsService.selectAll(bcDistrictsCity);
        BcCategoryDistricts bcCategoryModel = bcCategoryDistricts.stream().findFirst().orElse(new BcCategoryDistricts());
        req.setDistrict(Convert.toStr(bcCategoryModel.getId()));
        req.setDistrictCn(bcCategoryModel.getName());
    }

    @Override
    public List<NearbyResponse> nearby(Page page, NearbyRequest req) {
        return baseMapper.nearby(page, req);
    }
}
